<?php

	/*
	 * To change this license header, choose License Headers in Project Properties.
	 * To change this template file, choose Tools | Templates
	 * and open the template in the editor.
	 */

	namespace Phptricks\app\controllers\groups;

if (!defined('APPPATH')) {
		die("access denied not allowed");
	}
	/**
	 * Description of groups
	 *
	 * @author Abd Elfttah Ahmed <phptricks.net@gmail.com>
	 */
	use Phptricks\app\controllers\base\base as base;
	use Phptricks\app\models\groups\groups as groups_model;
	use Phptricks\app\models\users\users as users_model;

	class groups extends base{

		public $groups_model = null;
		public $users_model = null;

		public function __construct(){
			parent::__construct();
			$this->groups_model = groups_model::getinstance();
			$this->users_model = users_model::getinstance();
			$this->view->assign('page_title', $this->lang['groups_controll']);
		}

		public function admin_index(){
			$groups = $this->groups_model->get_groups('', 'group_id', true);
			$this->view->assign('groups', $groups);
			$this->view->display('groups.tpl');
		}

		public function admin_view(){

			$this->admin_index();
		}

		public function admin_edit($id){
			$id = \intval($id);
			$data = $this->groups_model->get_groups_by_id($id);
			if (\count($data) > 0) {
				$data = $data[0];
				if (!empty($data['group_options'])) {
					$data['group_options'] = $this->custom_fields->decode($data['group_options']);
				}
			}
			$this->view->assign('data', $data);
			$this->view->display('groups.tpl');
		}

		public function admin_update(){
			$this->form_validation->post(array(
				'group_id' => 'isnull|digit',
				'group_close' => 'isnull|digit',
				'group_name' => 'isnull',
				'group_options' => 'jsonencode',
			));

			$this->form_validation->fetch();
			$submit = $this->form_validation->submit();
			if ($submit !== true) {
				$url = 'admin/groups/edit/' . $this->form_validation->_array_data['group_id'];
				$this->display_message('danger', \implode('<br>', $this->form_validation->_error), $url, 3);
			}
			$array_where = array(
				'group_id' => $this->form_validation->_array_data['group_id']
			);
			$this->form_validation->unset_var('group_id', 'post');
			$this->groups_model->updatedata($this->form_validation->_array_data, $array_where);
			$this->display_message('success', $this->lang['success_edit_data'], 'admin/groups/view', 3);
		}

		public function admin_add(){
			$data = array();
			$data['group_options'] = $this->settings['default_group_settings'];
			$this->view->assign('data', $data);
			$this->view->display('groups.tpl');
		}

		public function admin_addnew(){
			$this->form_validation->post(array(
				'group_close' => 'isnull|digit',
				'group_name' => 'isnull',
				'group_options' => 'jsonencode',
			));

			$this->form_validation->fetch();
			$submit = $this->form_validation->submit();
			if ($submit !== true) {
				$this->display_message('danger', \implode('<br>', $this->form_validation->_error), 'admin/groups/add/', 5);
			}
			$this->form_validation->unset_var('group_id', 'post');
			$this->groups_model->addgroup($this->form_validation->_array_data);
			$this->display_message('success', $this->lang['success_add_data'], 'admin/groups/view/', 3);
		}

		public function admin_delete($id){
			if (!empty($id) && isset($id)) {
				if (\is_array($id)) {
					$id = \array_filter($id, function(&$id_val) {
						echo $id_val . "<br>";
						return \intval($id_val);
					});
				} else {
					$id = \intval($id);
				}
				$where_array = array(
					'group_id' => $id
				);
				$this->groups_model->deletegroups($where_array);
				$this->display_message('success', $this->lang['success_delete_data'], 'admin/groups/view/', 3);
			}
		}

		public function admin_action(){
			if ($this->form_validation->is_set('row_id') == false || $this->form_validation->is_set('action_type') == false) {
				$this->display_message('danger', $this->lang['error_select_data'], 'admin/groups/view/', 5);
			}
			$id = $this->form_validation->_post['row_id'];
			if (\is_array($id)) {
				$id = \array_filter($id, function(&$id_val) {
					return \intval($id_val);
				});
			} else {
				$id = \intval($id);
			}
			switch ($this->form_validation->_post['action_type']) {
				case 'del':

					if (!empty($id)) {
						$where_array = array(
							'group_id' => $id
						);
						try {
							$this->groups_model->deletegroups($where_array);
							$this->display_message('success', $this->lang['success_delete_data'], 'admin/groups/view/', 3);
						} catch (\Exception $exc) {
							throw new Exc($exc->getMessage());
						}
					}
					break;

				default:
					if (\intval($this->form_validation->_post['action_type']) > 0) {
						if (!empty($id)) {
							$where_array = array(
								'user_group_id' => $id
							);
							$updata_array = array(
								'user_group_id' => \intval($this->form_validation->_post['action_type'])
							);
							$this->users_model->updatedata($updata_array, $where_array);
							$this->display_message('success', $this->lang['success_move_user'], 'admin/groups/view/', 3);
						}
					} else {
						$this->display_message('danger', $this->lang['error_select_data'], 'admin/groups/view/', 5);
					}
					break;
			}
		}

	}
	